package cyc2018.二分查找.原理;

import java.util.Arrays;

/**
 * 时间复杂度为 O(logN)
 */
public class 正常实现 {

    public static boolean binarySearch(int[] nums, int target) {
        Arrays.sort(nums);
        int start = 0;
        int end = nums.length - 1;
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] > target) {
                end = mid - 1;
            } else if (nums[mid] < target) {
                start = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] a = {5,3,9,6,2,4,8,7,1,0};
        int target = 15;
        System.out.println(binarySearch(a, target));

    }
}
